﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;

namespace ERPU8WebApi.Until
{
    public static class SerializeLibrary
    {
        /// <summary>
        /// 利用反射来判断对象是否包含某个字段
        /// </summary>
        /// <param name="instance">object</param>
        /// <param name="propertyName">需要判断的字段</param>
        /// <returns>是否包含</returns>
        public static bool ContainProperty(this object instance, string propertyName)
        {
            if (instance != null && !string.IsNullOrEmpty(propertyName))
            {
                MemberInfo[] _findedPropertyInfo = instance.GetType().GetMember(propertyName);
                return (_findedPropertyInfo != null);
            }
            return false;
        }

        /// <summary>
        /// 获取对象属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="propertyname"></param>
        /// <returns></returns>
        public static string GetObjectPropertyValue<T>(T t, string propertyname)
        {
            Type type = typeof(T);
            PropertyInfo property = type.GetProperty(propertyname);
            if (property == null)
            {
                return string.Empty;
            }
            object o = property.GetValue(t, null);
            if (o == null)
            {
                return string.Empty;
            }
            return o.ToString();
        }

        /// <summary>
        /// 获取属性列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public static void GetObjectProperty<T>()
        {
            Type t = typeof(T);
            System.Reflection.PropertyInfo[] properties = t.GetProperties();
            foreach (System.Reflection.PropertyInfo info in properties)
            {
                Console.Write("name=" + info.Name + ";" + "type=" + info.PropertyType.Name + ";value=" + GetObjectPropertyValue<Object>(new object(), info.Name) + "<br />");
            }
        }

        public static Dictionary<string, string> GetFiledNameValue(object booc)
        {

            Dictionary<string, string> dic = new Dictionary<string, string>();

            foreach (var item in booc.GetType().GetFields())
            {
                if (item.GetValue(booc) != null)
                {
                    if (item.GetValue(booc).ToString().Contains("\""))
                    {
                        
                    }
                    dic.Add(item.Name.ToString(), "\"" + item.GetValue(booc).ToString() + "\"");
                }
                else
                {
                    dic.Add(item.Name.ToString(), "\"\"");

                }
            }

            return dic;
        }

        public static T D2E<T>(DataRow r)
        {
            T t = default(T);
            t = Activator.CreateInstance<T>();
            FieldInfo[] ps = t.GetType().GetFields();
            foreach (var item in ps)
            {
                if (r.Table.Columns.Contains(item.Name))
                {
                    object v = r[item.Name].ToString();
                    if (v.GetType() == typeof(System.DBNull))
                        v = null;
                    item.SetValue(t, v);
                }
            }
            return t;
        }

        public static Dictionary<string, string> D2E<T>(T r)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            FieldInfo[] ps = r.GetType().GetFields();
            foreach (var item in ps)
            {
                if (item.GetValue(r) != null)
                {
                    dic.Add(item.Name, item.GetValue(r).ToString());
                }
            }
            return dic;
        }
        
    }
}